from control.models import *
from literature.models import *
import pandas as pd
from django.db import IntegrityError, transaction
import json
from tqdm import tqdm
import traceback
from datetime import datetime
from basic_component_related_management.models import *


def init_phase_data():
    super_admin_entity = User.objects.filter(user_account="superadmin").first()
    unit_file_path = "files/basic_data_init_final_v2/A077. PCdata基表-5-相态表（20250403R1）-0版.xlsx"
    # unit_file_path = "files/basic_data_init_final_v1/基表20250306/A075test.xlsx"
    sheet_name = "二级相态表"
    df = pd.read_excel(unit_file_path, sheet_name=sheet_name, header=1, dtype=str)
    start_row = 1
    end_row = 50
    # end_row = 13
    num_columns = 9
    selected_data = df.iloc[start_row:, :num_columns].fillna("")

    # print(selected_data)
    phase_entity_list = []
    for index, row in tqdm(selected_data.iterrows(), desc="Processing phase data"):
        phase_entity = Phase(
            name_en=row["ePhaseName"],
            name=row["ePhaseNameCN"],
            abbreviation=row["sPhaseSymbol"],
            sei_id=row["nPhaseID"],
            is_pure_used=True,
            is_binary_used=True,
            is_ternary_used=True,
            is_multi_used=True,
            is_reaction_used=True,
            is_complex_used=True,
            description=row["sPhaseDescriptionCN"],
            description_en=row["sPhaseDescription"],
            comment=row["sPhaseComment"],
            create_person=super_admin_entity,
            modify_person=super_admin_entity,
        )
        phase_entity_list.append(phase_entity)
    try:
        with transaction.atomic():
            Phase.objects.bulk_create(phase_entity_list)
    except Exception as e:
        traceback.print_exc()


def init_phase_content():

    init_phase_data()

    print("phase finish")
